package tn.bergaoui.openwebapp.wicket.pages;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import tn.bergaoui.openwebapp.dao.PlayerDao;
import tn.bergaoui.openwebapp.model.Player;
import tn.bergaoui.openwebapp.wicket.components.ISelectionListener;

/**
 * Shows a list of people.
 * 
 * @author Bergaoui Bacem
 */
public class PlayerListPanel extends ListPanel<Player, PlayerDao> {
	
	public PlayerListPanel(String id, ISelectionListener listener,
			Component component) {
		super(id, listener, component);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SpringBean(name="playerDao")
	private PlayerDao playerDao;
	

	@Override
	protected Player createDefaultEntity() {
		Player player = new Player();
		player.setFirstName("new player");
		player.setLastName("noname");
		player.loadDefaultPicture();
		return player;
	}

	@Override
	protected ListView createListView(final Component component) {
		IModel hateList = new LoadableDetachableModel() {
			protected Object load() {
				return loadEntitis();
			}
		};
		
		ListView listView = new ListView("listview", hateList) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			protected void populateItem(ListItem item) {
				final Player player = (Player) item.getModelObject();
				AjaxLink link = new AjaxLink( "firstNameHyper") {
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						PlayerListPanel.this.listener.onSelectedItem(player.getId(), player.getClass());
						
						if (target !=null)  {
							target.addComponent(component);
						}
												
					}
				};
				
				
				link.add(new Label("firstName", player.getFirstName()));
				item.add(link);
				//item.add(new Label("firstName", player.getFirstName()));
				item.add(new Label("lastName", player.getLastName()));

			}

		};
		
		return listView;
	}

	@Override
	public PlayerDao getGenericDao() {
		return playerDao;
	}
	
	public PlayerDao getPlayerDao() {
		return playerDao;
	}

	public void setPlayerDao(PlayerDao playerDao) {
		this.playerDao = playerDao;
	}


}
